
from zhipuapi import *

def generate_conversation(drama: dict, output_file_path: str, round_time: int = 5):
    characters = drama['characters']
    play = drama['play']

    first_character = None
    second_character = None

    for character in characters:
        if character['name'] == play['first_character']:
            first_character = character
        else:
            second_character = character
    
    if first_character and second_character:
        scene = play['scene']
        first_line = play['first_line']

        query = f"（旁白：{scene}）{first_line}"
        output = f"{first_character['name']}: {query}"
        conversations = [output]
        yield output

        first_player = NovelCharecter(character=first_character, opponent=second_character)
        second_player = NovelCharecter(character=second_character, opponent=first_character)

        for _ in range(round_time):
            second_player_resp, succ = first_player.request(query)
            if succ:
                output = f"{second_character['name']}: {second_player_resp.lstrip()}"
                conversations.append(output)
                query = second_player_resp
                yield output
            else:
                break
            
            first_player_resp, succ = second_player.request(query)
            if succ:
                output = f"{first_character['name']}: {first_player_resp.lstrip()}"
                conversations.append(output)
                query = first_player_resp
                yield output
            else:
                break

        if len(conversations):
            with open(output_file_path, 'w') as f:
                f.write('\n'.join(conversations))
        